package com.hdaccp.ch04

import org.apache.spark.sql.SparkSession

/**
  * sql -> file
  */
object Demo2 {
  def main(args: Array[String]): Unit = {
    //1.得到一个SparkSession对象
    val spark = SparkSession.builder()
      .master("local[2]")
      .appName("ch04Demo2App")
      .getOrCreate()

    //2.也入隐式转换
    import  spark.implicits._

    //3.
    val dataFrame = spark.read.json("f:/resources/people.json")
    //4.主册临时视图
    dataFrame.createOrReplaceTempView("people")

    dataFrame.sqlContext.sql("select * from people").show()
    println("------------------------------------------")
    dataFrame.sqlContext.sql("select * from people where age>21").show()
    println("------------------------------------------")
    dataFrame.sqlContext.sql("select age,count(age) from people group by name").show()

    spark.stop()
  }
}
